//
//  AboutViewController.m
//  NetMobileLocation
//
//  Created by Mehmet Sinan Şahin on 3/10/12.
//  Copyright (c) 2012 netgis. All rights reserved.
//

#import "AboutViewController.h"
#import "SharedPrefs.h"
#import "LoginViewController.h"
#import "NGTestLogViewController.h"
#import "InformationViewController.h"
#import "NGUtility.h"


#define _BILGILER_SECTION 0
#define _GENEL_SECTION 1
#define _SUNUCU_URL_SECTION 2
#define _KONUM_GUNCELLEME_SECTION 3
#define _YETKI_BILGILERI_SECTION 4
#define _DUGME_SECTION 5


@implementation AboutViewController

@synthesize settingsTable;
@synthesize buttonsRow;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.title = TTLocalizedString(@"Settings", @"Ayarlar");
        self.tabBarItem.image = [UIImage imageNamed:@"settings"];
    }
    return self;
}

- (void)didReceiveMemoryWarning
{
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Release any cached data, images, etc that aren't in use.
}

#pragma mark - View lifecycle

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    settingsTable.delegate = self;
    settingsTable.dataSource = self;
    
}

- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
}

#pragma mark - login close delegate
-(void) loginClose {
    [settingsTable reloadData];
}

#pragma mark - table delegates
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    UITableViewCell *cell = nil;
    if (indexPath.section == _DUGME_SECTION) {
        cell = buttonsRow;
        return cell;
    }

    NSString *label = @"", *value = @"";
    if (indexPath.section == _BILGILER_SECTION) {
        if (indexPath.row == 0) {
            label = TTLocalizedString(@"Device ID", @"Device ID");
            value = [NGUtility getDeviceId];
        }
    } else if (indexPath.section == _GENEL_SECTION) {
        if (indexPath.row == 0) {
            label = TTLocalizedString(@"txtUSE_GPS", @"");
            value = [SharedPrefs useGPS] ? TTLocalizedString(@"Yes", @"") : TTLocalizedString(@"No", @"");
        } else if (indexPath.row == 1) {
            label = TTLocalizedString(@"txtSEND_SERVER", @"");
            value = [SharedPrefs isSendToServer] ? TTLocalizedString(@"Yes", @"") : TTLocalizedString(@"No", @"");;
        } else if (indexPath.row == 2) {
            label = TTLocalizedString(@"txtLOCATION_SIZE", @"");
            value = [NSString stringWithFormat:@"%d", [SharedPrefs getLocationSize]]; 
        } else if (indexPath.row == 3) {
            label = TTLocalizedString(@"txtSEND_FREQUENCY", @"");
            value = [NSString stringWithFormat:@"%d%@", [SharedPrefs getSendFrequency], TTLocalizedString(@"abbr min", @"")];
        }
    } else if (indexPath.section == _SUNUCU_URL_SECTION) {
        if (indexPath.row == 0) {
            label = TTLocalizedString(@"txtSERVER_URL", @"");
            value = [SharedPrefs getServiceUrl];                        
        }
    } else if (indexPath.section == _KONUM_GUNCELLEME_SECTION) {
        if (indexPath.row == 0) {
            label = TTLocalizedString(@"txtMIN_TIME", @"");
            value = [NSString stringWithFormat:@"%d%@", (int)[SharedPrefs getTimeInterval], TTLocalizedString(@"abbr meter", @"")];
        }        
        else if (indexPath.row == 1) {
            label = TTLocalizedString(@"txtMIN_DISTANCE", @"");
            value = [NSString stringWithFormat:@"%d%@", [SharedPrefs getDeltaDistance], TTLocalizedString(@"abbr meter", @"")];
        }        
    }  
    else if (indexPath.section == _YETKI_BILGILERI_SECTION) { //Yetki
        if (indexPath.row == 0) {
            label = TTLocalizedString(@"Session", @"");
            value = [SharedPrefs getSessionID];                       
        }        
        else if (indexPath.row == 1) {
            label = TTLocalizedString(@"User name", @"");
            value = [SharedPrefs getUserName];                     
        }   
        else if (indexPath.row == 2) {
            label = TTLocalizedString(@"Password", @"");
            value = @"******";       
        }
    }
    
    static NSString *cellIdentifier = @"SettingCell";
    cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
    
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:cellIdentifier] autorelease];            
    }
    
    cell.textLabel.text = label;
    cell.detailTextLabel.text = value;
    
    return cell;
    
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if (section == _BILGILER_SECTION) {
        return 1;
    } else if (section == _GENEL_SECTION) {
        return 4;
    } else if (section == _SUNUCU_URL_SECTION) {
        return 1;
    } else if (section == _KONUM_GUNCELLEME_SECTION) {
        return 2;
    } else if (section == _YETKI_BILGILERI_SECTION) {
        return 3;
    }
    return 1;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 6;
}

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
    if (section == _BILGILER_SECTION) {
        return @"";//@"Bilgiler";
    } else if (section == _GENEL_SECTION) {
        return  @"";//@"Genel";
    } else if (section == _SUNUCU_URL_SECTION) {
        return  @"";//@"";
    } else if (section == _KONUM_GUNCELLEME_SECTION) {
        return  @"";//@"Konum Güncelleme";
    } else if (section == _YETKI_BILGILERI_SECTION) {
        return  @"";//@"Yetki Bilgileri";
    }
    return @"";
}

- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == _DUGME_SECTION) {
        [cell setSelectionStyle:UITableViewCellSelectionStyleNone];
        UIView *backView = [[UIView alloc] initWithFrame:CGRectZero];
        backView.backgroundColor = [UIColor clearColor];
        cell.backgroundView = backView;
        [backView release];     
    }
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section == _BILGILER_SECTION && indexPath.row == 0) { //device id
        InformationViewController *cont = [[InformationViewController alloc] initWithNibName:@"InformationViewController" bundle:nil];
        
        [self.view addSubview:cont.view];        
    } else if (indexPath.section == _SUNUCU_URL_SECTION && indexPath.row == 0) { //server url
        [NGUtility alert:[SharedPrefs getServiceUrl] title:TTLocalizedString(@"txtSERVER_URL", @"")];     
    }
}

#pragma mark - button actions
-(IBAction)toLogin:(id)sender {
    LoginViewController *login = [[LoginViewController alloc] initWithNibName:@"LoginViewController" bundle:nil];
    login.forStartup = FALSE;
    [login setCloseCallbackTarget:self];
    [self.view addSubview:login.view];
}

-(IBAction)test:(id)sender {
    NGTestLogViewController *cont = [[NGTestLogViewController alloc] initWithNibName:@"NGTestLogViewController" bundle:nil];
    [self.view addSubview:cont.view];
}

- (void)dealloc {
    [settingsTable release];
    [buttonsRow release];
    [super dealloc];
}

@end
